package com.yellowcong.http.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.yellowcong.http.dto.UserDto;
import com.yellowcong.http.model.Lession;
import com.yellowcong.http.model.User;
import com.yellowcong.http.service.UrpService;
import com.yellowcong.http.test.HttpClient;
import com.yellowcong.http.values.URP;
import com.yellowcong.utils.StringUtils;

public class UrpServiceImpl implements UrpService{

	@Override
	public User login(UserDto dto) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("zjh", dto.getStudentId());
		map.put("mm",dto.getPasswrod());
		String str = HttpClient.post(URP.URP_URL,map);

		String strTop = HttpClient.get(URP.MENU_TOP_URL);
		//获取到所有的用户的菜单名称
		Document doc = Jsoup.parse(strTop);
		//获取用户菜单
		List<String> menus = getTopMenus(doc);
		//获取用户名称
		String username = getUserName(doc);
		
		User user = new User();
		user.setPassword(dto.getPasswrod());
		user.setStudentId(dto.getStudentId());
		user.setUsername(username);
		user.setTopMenu(menus);
		
		return user;
	}
	
	/**
	 * 获取同学的学期信息，一个学期就是一个成绩啊
	 * 获取同学们的学期
	 *	2012-2013学年第一学期(两学期)
		2012-2013学年第二学期(两学期)
		2013-2014学年第一学期(两学期)
		2013-2014学年第二学期(两学期)
		2014-2015学年第一学期(两学期)
		2014-2015学年第二学期(两学期)
		2015-2016学年第一学期(两学期)
		2015-2016学年第二学期(两学期) 
	 * @return
	 */
	public List<String> getXqInfo(){
		String quanBuStr = HttpClient.get(URP.INFO_QB_JG_URL);
		Document  quanBu = Jsoup.parse(quanBuStr);
		Element el = quanBu.getElementsByClass("table_k").get(0);
		Elements dataInfo = el.getElementsByTag("table").last().getElementsByTag("a");
		Iterator<Element> its = dataInfo.iterator();
		List<String> list = new ArrayList<String>();
		while(its.hasNext()){
			list.add(its.next().html());
		}
		return list;
	}
	
	/**
	 * 移除掉String上面的html数据
	 * @param el
	 * @return
	 */
	private String formatStr(Element el){
		 //去掉html的样式
		 String info = StringUtils.removeHtmlTags(el.html());
		 return StringUtils.removeHtmlNull(info);
	}
	
	/**
	 * 设定学习数据消息  
	 * @param lnx
	 * @param xueqiNms
	 * @param xueqi
	 * @return
	 */
	private List<Lession> setLession(Element lnx,String xqNames){
		List<Lession> lessions = new ArrayList<Lession>();
		
		//获取每个学期的表头,表头固定的，所以没有动
		Elements xqEl = lnx.getElementsByTag("thead").get(0).getElementsByTag("th");
		Iterator<Element> xqElIt = xqEl.iterator();
		
		//获取每个学期的成绩
		Elements scorEl = lnx.getElementsByTag("tbody").first().getElementsByTag("tr");
		Iterator<Element> scorIt = scorEl.iterator();
		while(scorIt.hasNext()){
			 //获取一个课程的所有信息
			 Elements lessionEls = scorIt.next().getElementsByTag("td");
			 
			 Lession les = new Lession();
			
			 //学期
			 les.setXueqi(xqNames);
			 //课程号
			 les.setCode(formatStr(lessionEls.get(0)));
			 //课序号
			 les.setNumber(formatStr(lessionEls.get(1)));
			 //课程名 
			 les.setCnName(formatStr(lessionEls.get(2)));
			 //英文课程名 
			 les.setEnName(formatStr(lessionEls.get(3)));
			 // 学分
			 les.setCredit(formatStr(lessionEls.get(4)));
			 //课程属性
			 les.setType(formatStr(lessionEls.get(5)));
			 //成绩
			 les.setScore(formatStr(lessionEls.get(6)));
			 if(xqNames == null || "".equals(xqNames)){
				 //未通过原因 
				 les.setResion(formatStr(lessionEls.get(7)));
			 }
			 //添加数据
			 lessions.add(les);
		}
		return lessions;
	}
	/**
	 * 这个破玩意人家设计的时候,就是将所有的学期的数据都发送给我们
	 * @return
	 */
	public List<Lession> getLessionScoreAll(){
		return this.getLessionScore(null);
		
	}
	/**
	 * 获取每个学期成绩的url
	 * gradeLnAllAction.do?type=ln&oper=qbinfo&lnxndm=2015-2016学年第二学期(两学期)#qb_2015-2016学年第二学期(两学期)
	 * @param xueqi
	 * @return
	 */
	private String getLnxndmUrl(String xueqi){
		return URP.INFO_QB_LNXNDM_URL+"&lnxndm="+xueqi+"#qb_"+xueqi;
	}
	/**
	 * 获取用户的名称
	 * @param doc
	 * @return
	 */
	private static String getUserName(Document doc){
		//获取用户的信息
		Element userInfo=doc.getElementsByClass("leftuser01").get(0);
		String userNameStr =userInfo.select("tr").first().text();
		userNameStr = userNameStr.replace("欢迎光临", "").replace("|", "").replace("注销", "").replaceAll(" ", "").replaceAll(" ", "");
		return userNameStr;
	}
	/**
	 * 获取顶部的菜单数据
	 * @param doc
	 * @return
	 */
	private static List<String> getTopMenus(Document doc){
		Elements topMenus= doc.getElementsByTag("A");
		Iterator<Element> it = topMenus.iterator();
		List<String> list = new ArrayList<String>();
		while(it.hasNext()){
			list.add(it.next().html());
		}
		return list;
	}

	@Override
	public List<Lession> getLessionScore(Integer xueqi) {
		List<String> xueqiNms = getXqInfo();
		Integer xueqiNow = null;
		if(xueqi == null){
			xueqiNow = xueqiNms.size()-1;
		}else{
			xueqiNow = xueqi-1;
		}
		//获取访问的url
		String lnxUrl = getLnxndmUrl(xueqiNms.get(xueqiNow));
		List<Lession> lessions = new ArrayList<Lession>();
		
		//获取学期数据
		String lnxHtml = HttpClient.get(lnxUrl);
		Document lnxDoc = Jsoup.parse(lnxHtml);
		
		Elements lnxEls = lnxDoc.getElementsByClass("displayTag");
		
		
		for(int i=0;i<=xueqiNow;i++){
			Element lnx = lnxEls.get(i);
			//获取这学期的课程
			List<Lession> xqLession = setLession(lnx,xueqiNms.get(i));
			lessions.addAll(xqLession);
		}
		return lessions;
	}

	@Override
	public List<Lession> getLessionScoreByType() {
		//通过属性查询成绩
		String strHtml = HttpClient.get(URP.INFO_SX_URL);
		Document lnxDoc = Jsoup.parse(strHtml);
		Elements lnxEls = lnxDoc.getElementsByClass("displayTag");
		
		Iterator<Element> lnxIt = lnxEls.iterator();
		List<Lession> lessions = new ArrayList<Lession>();
		
		while(lnxIt.hasNext()){
			Element lnx =lnxIt.next();
			List<Lession> xqLession = setLession(lnx,"");
			lessions.addAll(xqLession);
		}
		
		return lessions;
	}

	@Override
	public List<Lession> getLessionScoreByMajor() {
		//通过属性查询成绩
		String strHtml = HttpClient.get(URP.INFO_FA_URL);
		Document lnxDoc = Jsoup.parse(strHtml);
		Elements lnxEls = lnxDoc.getElementsByClass("displayTag");
		
		Iterator<Element> lnxIt = lnxEls.iterator();
		List<Lession> lessions = new ArrayList<Lession>();
		
		while(lnxIt.hasNext()){
			Element lnx =lnxIt.next();
			List<Lession> xqLession = setLession(lnx,"");
			lessions.addAll(xqLession);
		}
		
		return lessions;
	}

	
}
